package com.lap.framework.generator.application.assembler;

import com.lap.framework.generator.domain.template.CodeTemplate;
import com.lap.framework.generator.domain.valueobject.CodeType;
import java.util.*;
import lombok.experimental.UtilityClass;

@SuppressWarnings("all")
@UtilityClass
public class Assembler {

  private static final Map<CodeType, List<CodeTemplate>> GENERATOR_MAP =
      new EnumMap<>(CodeType.class);

  static {
    ServiceLoader<CodeTemplate> loader = ServiceLoader.load(CodeTemplate.class);
    for (CodeTemplate<?> generator : loader) {
      if (GENERATOR_MAP.containsKey(generator.getType())) {
        GENERATOR_MAP.get(generator.getType()).add(generator);
      } else {
        var list = new ArrayList<CodeTemplate>();
        list.add(generator);
        GENERATOR_MAP.put(generator.getType(), list);
      }
    }
  }

  public static List<CodeTemplate> getGenerator(CodeType codeType) {
    List<CodeTemplate> generators = GENERATOR_MAP.get(codeType);
    if (generators == null) {
      throw new IllegalArgumentException("No generator found for type: " + codeType);
    }
    return generators;
  }
}
